//
//  LeftViewController.m
//  CSLeftSlideDemo
//
//  Created by LCS on 16/2/11.
//  Copyright © 2016年 LCS. All rights reserved.
//

#import "LeftViewController.h"
#import "Constants.h"
#import <UIImageView+WebCache.h>
@interface LeftViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIImageView *headerImg;
@property (weak, nonatomic) IBOutlet UILabel *nickname;

@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) NSArray *imgArr;
@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _dataArr = @[@"我的订单",@"优惠券",@"邀请有奖",@"设置"];
    _imgArr = @[@"order", @"coupon", @"gift", @"setting"];
    //接受通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNickname:) name:@"changeUserNickname" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeUserHeader:) name:@"changeUserHeader" object:nil];
    self.headerImg.layer.cornerRadius = 30.0;
    self.headerImg.layer.masksToBounds = YES;
    
    
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"login"]) {
        NSDictionary *user = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
        [self.headerImg sd_setImageWithURL:[NSURL URLWithString:user[@"imgs"][0]] placeholderImage:[UIImage imageNamed:@"zhangweitu"]];
        self.nickname.text = user[@"nickname"];
    }
    
    [self.headerView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(login)]];
}

- (void)changeNickname:(NSNotification *)info {
    if (info.object) {
        self.nickname.text = info.object;
    }
    else {
        self.nickname.text = @"请登录";
    }
}

- (void)changeUserHeader:(NSNotification *)info {
    if (info.object) {
        [self.headerImg sd_setImageWithURL:[NSURL URLWithString:info.object] placeholderImage:[UIImage imageNamed:@"zhangweitu"]];
    }
    else {
        self.headerImg.image = [UIImage imageNamed:@"default_head"];
    }
}

- (void)login {
    if (self.delegate && [self.delegate respondsToSelector:@selector(LeftViewControllerdidSelectRow:)]) {
        //已经登录则进入用户界面
        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"login"]) {
            [self.delegate LeftViewControllerdidSelectRow:LeftViewControllerRowTypeAccount];
        }
        //如果没有登录，则进入登录界面
        else {
            [self.delegate LeftViewControllerdidSelectRow:LeftViewControllerRowTypeLogin];
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cell_id = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cell_id];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.imageView.image = [UIImage imageNamed:_imgArr[indexPath.row]];
    [cell.textLabel setTextColor:[UIColor colorWithRed:0x8b / 255.0 green:0xa3 / 255.0 blue:0xb6 / 255.0 alpha:1.0]];
    [cell.textLabel setText:_dataArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 52;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self deSelectCell];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(LeftViewControllerdidSelectRow:)]) {
        NSInteger row = indexPath.row;
        [self.delegate LeftViewControllerdidSelectRow:row];
    }
}

- (void)deSelectCell
{
    [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:NO];
}
- (IBAction)userHelpBtn:(UIButton *)sender {
    [self.delegate LeftViewControllerdidSelectRow:5];
}
- (IBAction)aboutUsBtn:(UIButton *)sender {
    [self.delegate LeftViewControllerdidSelectRow:6];
}

@end
